<template>
  <div id="bgs">
  <div class="pay-tool" >
    <p class="content" style="padding: 1rem 0 1rem 0">请您设置6位数交易密码，交易密码再订单支付时使用</p>

    <div class="pay-tool-content">
      <div class="pay-tool-inputs">
        <div class="item" v-for="i in items"><span class="icon_dot" v-if="password[i]"></span></div>
      </div>
      <!--<div class="pay-tool-link"><router-link class="link" to="/getP">忘记密码？</router-link></div>-->
    </div>

    <div class="pay-tool-keyboard" v-show="paytoolkeyboard">
      <ul>
        <li @click="keyUpHandle($event)" v-for="val in keys">
          {{ val }}
        </li>
        <li class="del" @click="delHandle"><span class="icon-del" style="color: #528CE0">删除</span></li>
      </ul>
    </div>


    <div class="tishiAgree" v-show="tishiShow">
      <p>{{tishiInfor}}</p>
    </div>
  </div>
  </div>

</template>

<script>

  import axios from 'axios'
  const keys = () => [1, 2, 3, 4, 5, 6, 7, 8, 9, '', 0]
  // let sendFlag = true // 防止重复发送密码
  export default {
    data () {
      return {
        AuthTokenPwd:'',
        items: [0, 1, 2, 3, 4, 5],
        keys: keys(),
        password: [],
        pwds:'',
        paytoolkeyboard:true,
        tishiShow:false,
        tishiInfor:''
      }
    },
    created() {
      var that = this;
      that.AuthTokenPwd = sessionStorage.getItem('AuthToken');
    },
    methods: {
      keyUpHandle (e) {
        let text = e.currentTarget.innerText;
        let len = this.password.length;
        if (!text || len >= 6)
          this.clearPasswordHandle()
        this.password.push(text)
        this.ajaxData();
      },
      delHandle () {
        if (this.password.length <= 0) return false
        this.password.shift()
      },
      ajaxData () {
        if (this.password.length >= 6) {
          // console.log(parseInt(this.password.join(' ').replace(/\s/g, '')))
          var pwds=parseInt(this.password.join(' ').replace(/\s/g, ''))
          this.pwds=pwds;
          console.log(pwds);

          if(this.password==''){
            // alert('密码不能为空');
            this.tishiShow = true;
            this.tishiInfor = '密码不能为空';
            var _that = this;
            window.setTimeout(function () {
              _that.tishiShow = false
            }, 2000);
          }else {
            var that=this;
            axios.post('http://test.zjapi.xianxiangle.com/set_pay_psw',{AuthToken: that.AuthTokenPwd,pay_passwd:that.pwds}).then(function (res) {
              console.log(res.data)

              that.tishiShow = true;
              that.tishiInfor =res.data.message;
              window.setTimeout(function () {
                that.password=[]
                that.tishiShow = false
              }, 2000);
            }).catch(function (err) {
            })
          }

            // this.paytoolkeyboard=false
        }
        // return false
      },
      clearPasswordHandle: function () {
        this.password = []
      }
    },

  }
</script>
<style lang="scss" scoped>
  #bgs{
    width: 100%;
    height: auto;
    background: #cccccc;
  }
  .tishiAgree {
    width: 100%;
    /*display: none;*/
    margin: auto;
    p {
      width: 50%;
      background: rgba(0, 0, 0, 0.4);
      filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#66ffffff, endColorstr=#66ffffff);
      border-radius: 0.3rem;
      color: white;
      padding: 0.5rem;
      text-align: center;
      height: 2rem;
      line-height: 2rem;
      font-size: 1.6rem;
      margin: auto;
    }
  }
  .pay-tool {
    position: relative;
    height:auto;
    background-color: #fff;
    overflow: hidden;
    /*border: 1px solid;*/
    &-title {
      width: 100%;
      height: 2.08888888rem;
      padding: 0 0.8rem;
      line-height: 2.08888888rem;
      text-align: center;
      overflow: hidden;
      .icon {
        float: left;
        margin-top: 0.72222222rem;
      }
      strong {
        font-size: 0.8rem;
      }
    }
    &-content {
      .pay-tool-inputs {
        width: 20rem;
        height: 4rem;
        margin: 1.28888888rem auto 0;
        /*border: 1px solid #b9b9b9;*/
        border-radius: 0.26666666rem;
        box-shadow: 0 0 1px #e6e6e6;
        display: flex;
        .item {
          width: 16.66666666%;
          height: 2.31111111rem;
          border-right: 1px solid #eeeeee;
          line-height: 2.31111111rem;
          text-align: center;
          &:last-child {
            border-right: none;
          }
          .icon_dot {
            display: inline-block;
            width: 0.51111111rem;
            height: 0.51111111rem;
            background: url("../assets/i7_detail_16g@2x_01.png") no-repeat;
            background-size: cover;
          }
        }
      }
      .pay-tool-link {
        padding: 0.53333333rem 0.8rem 0;
        text-align: right;
        .link {
          font-size: 0.66666666rem;
          color: #3c8cfb;
        }
      }
    }
    .pay-tool-keyboard {
      /*position: absolute;*/
      /*left: 0;*/
      /*bottom: 0;*/
      width: 100%;
      height: auto;
      text-align: center;
      margin: auto;

      ul {
        width: 60%;
        display: flex;
        list-style: none;
        flex-wrap: wrap;
        text-align: center;
        margin: auto;
        margin-top: 2rem;
        /*border: 1px solid red;*/
        li {
          width: 26%;
          height: 3rem;
          line-height:3rem;
          text-align: center;
          /*border-right: 1px solid #aeaeae;*/
          /*border-bottom: 1px solid #aeaeae;*/
          border: 1px solid #eeeeee;
          font-size: 0.8rem;
          font-weight: bold;
          margin: auto;
          margin-top: 0.2rem;
          /*margin: 0.1rem;*/
          &:nth-child(10), &:nth-child(12), &:active {
            background-color: #d1d4dd;
          }
          &:nth-child(12):active {
            background-color: #fff;
          }
        }
      }
    }
  }
</style>
